package com.kfm.pm;

import java.sql.*;

public class JDBCDemo03 {

    public static void main(String[] args) {
        // 1.注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 2. 获取连接
        String url = "jdbc:mysql://localhost:3306/store?user=root&password=root";
        try (Connection conn = DriverManager.getConnection(url);
             // 3. 获取执行sql的对象
             Statement stat = conn.createStatement();) {
            // 4. 编写sql
            String sql = "select id no, phone, password, type, name, point, money, create_time from account";
            // 5. 执行sql
            ResultSet set = stat.executeQuery(sql);
            // 获取列数
            ResultSetMetaData metaData = set.getMetaData();
            int columnCount = metaData.getColumnCount(); // 3 1,2,3   5 1,2,3,4,5
            System.out.println("列数:" + columnCount);
            String columnClassName = metaData.getColumnClassName(1); // 第一列对应的java类型
            System.out.println(columnClassName);
            String catalogName = metaData.getCatalogName(1);
            System.out.println(catalogName); // 数据库名称
            int columnDisplaySize = metaData.getColumnDisplaySize(1);
            System.out.println(columnDisplaySize);
            String columnLabel = metaData.getColumnLabel(1);
            String columnName = metaData.getColumnName(1);
            System.out.println("label" + columnLabel); // no 如果起别名了 就是别名
            System.out.println("name" + columnName); // name 数据库中的原始字段名
            int columnType = metaData.getColumnType(2); // 获取对应的数据库数据类型 数字格式
            System.out.println(columnType);
            String columnTypeName = metaData.getColumnTypeName(2); // 获取对应的数据库数据类型
            System.out.println(columnTypeName);
            String schemaName = metaData.getSchemaName(1);
            System.out.println(schemaName);
            String tableName = metaData.getTableName(1);
            System.out.println(tableName); // 获取表名称
            boolean autoIncrement = metaData.isAutoIncrement(1);
            System.out.println(autoIncrement); // 是否自增
            boolean caseSensitive = metaData.isCaseSensitive(1);
            System.out.println(caseSensitive); //
            // 6. 解析结果
//            while (set.next()) { // 类比 hesNext() next() 整合到一起了
////                Object object = set.getObject("no");
////                Object object1 = set.getObject("name");
////                Object object2 = set.getObject("loc");
////                System.out.println(object + " " + object1 + " " + object2);
//                for (int i = 1; i <= columnCount; i++) {
//                    Object value = set.getObject(i);
//
////                    System.out.print(value == null ? " " : value.getClass() + " ");
//                    System.out.print(value + " ");
//                }
//                System.out.println();
//            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
